package com.rpcfx.server;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: learning
 * @Package: com.rpcfx.server
 * @ClassName: ReflectiveInvoker
 * @Description:
 * @Date: 2021/11/11 10:55 下午
 * @Version: 1.0
 */
public class ReflectiveInvoker {

    private Object target;

    public ReflectiveInvoker(Object target) {
        this.target = target;
    }

    public Object invoke(String methodName, Object[] params) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        List<Class<?>> paramTypeList = new ArrayList<>();
        for (Object param : params) {
            paramTypeList.add(param.getClass());
        }
        // 目前 包裹类型Integer.class to int.class 有问题。
        Method method = target.getClass().getDeclaredMethod(methodName, paramTypeList.toArray(new Class[0]));
        method.setAccessible(true);
        return method.invoke(target, params);
    }
}
